package persistence.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import persistence.db.DataSet;
import persistence.db.DatabaseConnector;
import persistence.model.AbstractModel;
import persistence.model.TypeHeure;

public class TypeHeureDAO extends AbstractDAO{
	
	private TypeHeure typeHeure;

	/**
	 * Constructeur d'un type heure DAO
	 */
	public TypeHeureDAO() {
		setTableName("TYPE_HEURE");
	}
	
	/**
	 * Constructeur d'un type d'heure DAO
	 * @param typeH TypeHeure correspondant
	 */
	public TypeHeureDAO(TypeHeure typeH){
		this();
		typeHeure = typeH;
		setId(typeHeure.getId());
	}

	public DataSet all() {
		return new DataSet(this);
	}

	public AbstractModel makeModel(ResultSet rset) {
		try {
			setId(rset.getInt("id_type_heure"));
			return new TypeHeure(getId(),
					rset.getString("nomTypeHeure"));
		} catch (SQLException e) {
			System.out.println("makeModel error");
			e.printStackTrace();
		}
		return null;
	}

	public void save() {
		String query;
		ResultSet result;
		query = "INSERT INTO " + getTableName() + " VALUES ( S_PK_" + getTableName() + ".nextval, '"
		+ typeHeure.getNomTypeHeure() +"')";
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
		setId(this.getId()); /* Mise a jour de l'identifiant */
	}

	public void save(int id) {
		String query;
		ResultSet result;
		query = "UPDATE " + getTableName() +
			" SET nomTypeHeure = '" + typeHeure.getNomTypeHeure() +
			"' WHERE ID_"+ getTableName() + " = "+ id;
		/* Execute la requete */
		result = DatabaseConnector.getInstance().execute(query);
	}
	
}
